package com.senseuni.da.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.senseuni.common.dialog.CBDialogBuilder;
import com.senseuni.common.dialog.OnDialogResultListener;
import com.senseuni.da.R;
import com.senseuni.da.adapter.SimpleRecyclerAdapter;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration;

import java.util.List;

/**
 * ================================================
 * 描述：列表选择弹窗, 有确定按钮，左上角带 没有 关闭按钮
 * 用法：
 * <p>
 *        new SimpleListDialog(this, list)
 *                 .setOnResultListener(new OnDialogResultListener<String>() {
 *                     @Override
 *                     public void onResult(Dialog dialog, String result) {
 *                     }
 *                 })
 *                 .create().show();
 * </p>
 * ================================================
 */
public class ChoiceListDialog extends CBDialogBuilder {

    private List<SimpleListItem> mDataList;
    private String title;

    private int mChoiceIndex = 0;

    public ChoiceListDialog(Context context, List<SimpleListItem> list) {
        this(context, context.getString(R.string.tip), list, 0);
    }

    public ChoiceListDialog(Context context, List<SimpleListItem> list, int defaultChoiceIndex) {
        this(context, context.getString(R.string.tip), list, defaultChoiceIndex);
    }

    public ChoiceListDialog(Context context, String title, List<SimpleListItem> list, int defaultChoiceIndex) {
        super(context, CBDialogBuilder.DIALOG_STYLE_NORMAL, (float) 0.4);
        this.mChoiceIndex = defaultChoiceIndex;
        this.mDataList = list;
        this.title = title;
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_dialog_simple_list, null);
        SwipeRecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(new DefaultItemDecoration(Color.parseColor("#FFE5E5E5"), 1, 1));
        recyclerView.setOnItemClickListener((view1, adapterPosition) -> {
            getDialog().dismiss();
            if (mListener != null) {
                mListener.onResult(getDialog(), mDataList.get(adapterPosition));
            }
        });

        SimpleRecyclerAdapter<SimpleListItem> adapter = new SimpleRecyclerAdapter<SimpleListItem>(recyclerView, mDataList) {

            @Override
            public int getItemLayout() {
                return R.layout.view_item_choice_list_dialog;
            }

            @Override
            public void onBindData(@NonNull com.senseuni.da.adapter.ViewHolder viewHolder, SimpleListItem data, int position) {
                RadioButton radioButton = viewHolder.getView(R.id.rb_content);
                radioButton.setText(data.value);
                radioButton.setChecked(position == mChoiceIndex);
            }
        };
        recyclerView.setAdapter(adapter);
        setTitle(title);
        setView(view);
        setTouchOutSideCancelable(true);
        showCancelButton(false);
        setConfirmButtonText(context.getString(R.string.yv_cancel));
        setButtonClickListener(true, null);
    }

    public void show() {
        getDialog().show();
    }

    private OnDialogResultListener<SimpleListItem> mListener;

    public ChoiceListDialog setOnResultListener(OnDialogResultListener<SimpleListItem> listener) {
        mListener = listener;
        return this;
    }

}
